package com.drcoffee.drcoffeemall.controller;

import com.drcoffee.drcoffeemall.entry.Attachment;
import com.drcoffee.drcoffeemall.entry.Result;
import com.drcoffee.drcoffeemall.entry.User;
import com.drcoffee.drcoffeemall.service.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 收藏
 *
 * @author ShiHao Zhou
 */
@RestController
@RequestMapping("/collect")
public class CollectController {
    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    CollectService collectService;

    /*
     * 根据用户查询收藏
     * */
    @GetMapping("/getCollect")
    public Result getCollect(@RequestHeader("Token") String Token, @RequestParam String search, @RequestParam String language) {
        return collectService.getCollect(Token, search, language);
    }

    /*
     * 添加至收藏
     * */
    @PostMapping("/addToCollect")
    public Result addToCollect(@RequestHeader("Token") String Token, @RequestBody Attachment attachment) {
        return collectService.addToCollect(Token, attachment);
    }

    /*
     * 从收藏中删除
     * */
    @GetMapping("/removeFromCart")
    public Result removeFromCart(@RequestHeader("Token") String Token, @RequestParam("part_code") List<String> part_code) {
        return collectService.removeFromCart(Token, part_code);
    }

    /*
     * 修改收藏中数量
     * */
    @PostMapping("/updateQuantityById")
    public Result updateQuantity(@RequestHeader("Token") String Token, @RequestBody Attachment attachment) {
        User user = (User) redisTemplate.opsForValue().get(Token);
        return collectService.updateQuantityById(user.getAccount(), attachment);
    }

    /*
     * 获取收藏选中项合计金额
     * */
    @PostMapping("/getTotal")
    public ResponseEntity<Object> getTotal(@RequestHeader("Token") String Token, @RequestBody List<String> part_codes) {
        return collectService.getTotal(Token, part_codes);
    }

}
